import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/ip")
public class Demo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        InputStream inputStream = getClass().getResourceAsStream("/ips.txt");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        Map<String,Integer> map = new HashMap<>(); // {k1:v1,k2:v2}

        while (true){
            String ip = bufferedReader.readLine();

            if (bufferedReader.readLine()==null){
                break;
            }
            if (map.containsKey(ip)){
              int i =  map.get(ip)+1;
                map.put(ip,i);
            }else {
                map.put(ip,1);
            }

        }
        String s = JSON.toJSONString(map);
        resp.getWriter().print(s);
    }
}
